﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessLibrary.BLL;
using System.Data.SqlClient;

namespace BusinessLibrary.DAL
{
    class sqlGouvernorat
    {
         public static Gouvernorat ReaderToObject(SqlDataReader dr)
        {
            Gouvernorat _gouvernorat = new Gouvernorat();
            _gouvernorat.Id = int.Parse(dr["Id"].ToString());

            _gouvernorat.Nom = dr["Nom"].ToString();
       

         
            if (!object.ReferenceEquals(dr["Id_pays"], DBNull.Value))
            {
                _gouvernorat.pays = Pays.GetPaysById(int.Parse(dr["Id_pays"].ToString()));
            }
            return _gouvernorat;
        }

        public static Gouvernorat GetGouvernoratById(int id)
        {
            Gouvernorat gouvernorat = new Gouvernorat();
            string strSQL = @"select * from Gouvernorat where UserId=@Id";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);
                    cmd.Parameters.AddWithValue("@Id", id);
                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        gouvernorat = ReaderToObject(dr);
                    }
                    dr.Close();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return gouvernorat;
        }

        public static int insertGouvernorat(Gouvernorat g)
        {
            int n = 0;
            string strSQL = @"insert into Gouvernorat (Nom,Id_pays) values(@nom, @id_pays)";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);
           
                    cmd.Parameters.AddWithValue("@nom", g.Nom);
                    cmd.Parameters.AddWithValue("@id_pays", g.pays.Id);

                    n = cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int updateGouvernorat(Gouvernorat g)
        {
            int n = 0;

            string strSQl = @"UPDATE Gouvernorat SET (@nom IS NULL OR Nom=@nom) Id_pays=@id_pays where Id=@id ";
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();

                    SqlCommand cmd = new SqlCommand(strSQl, cnx);
          
                    cmd.Parameters.AddWithValue("@nom", g.Nom);
                    cmd.Parameters.AddWithValue("@id_payst",g.pays.Id);
                    n = cmd.ExecuteNonQuery();

                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="idAuthentif"></param>
        /// <returns></returns>
        public static int deletGouvernorat(int id)
        {
            int n = 0;
            string strSQL = @"delete from Gouvernorat where id=@id)";
           
            try
            {
                using (SqlConnection cnx = new SqlConnection(DALtools.DataConnectionString))
                {
                    cnx.Open();
                    SqlCommand cmd = new SqlCommand(strSQL, cnx);
                    
                    cmd.Parameters.AddWithValue("@id", id);
                    
                   
                   n=  cmd.ExecuteNonQuery();
                    cnx.Close();
                }
            }
            catch (Exception) { }
            return n;
        }
    }
    }
